package org.jix.weblog.web.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jix.weblog.module.common.domain.dos.ArticleDO;
import org.jix.weblog.module.common.domain.dos.ArticleTagRelDO;
import org.jix.weblog.module.common.domain.dos.TagDO;
import org.jix.weblog.module.common.domain.mapper.ArticleMapper;
import org.jix.weblog.module.common.domain.mapper.ArticleTagRelMapper;
import org.jix.weblog.module.common.domain.mapper.TagMapper;
import org.jix.weblog.module.common.enums.ResponseCodeEnum;
import org.jix.weblog.module.common.exception.BizException;
import org.jix.weblog.module.common.utils.PageResponse;
import org.jix.weblog.module.common.utils.Response;
import org.jix.weblog.web.convert.ArticleConvert;
import org.jix.weblog.web.model.vo.tag.FindTagArticlePageListReqVO;
import org.jix.weblog.web.model.vo.tag.FindTagArticlePageListRspVO;
import org.jix.weblog.web.model.vo.tag.FindTagListRspVO;
import org.jix.weblog.web.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @Author Jix
 * @Date 2024/9/10 18:58
 * @注释
 */

@Service
@Slf4j
public class TagServiceImpl implements TagService {

    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleTagRelMapper articleTagRelMapper;

    /**
     * 获取标签列表
     * @return
     */
    @Override
    public Response findTagList() {
        //查询所有标签
        List<TagDO> tagDOS = tagMapper.selectList(Wrappers.emptyWrapper());

        //DO转VO
        List<FindTagListRspVO> vos = null;
        if(!CollectionUtils.isEmpty(tagDOS)){
            vos = tagDOS.stream()
                    .map(tagDO -> FindTagListRspVO.builder()
                            .id(tagDO.getId())
                            .name(tagDO.getName())
                            .build())
                    .collect(Collectors.toList());
        }

        return Response.success(vos);
    }

    /**
     * 获取标签下文章分页列表
     * @param findTagArticlePageListReqVO
     * @return
     */
    @Override
    public Response findTagPageList(FindTagArticlePageListReqVO findTagArticlePageListReqVO) {

        Long current = findTagArticlePageListReqVO.getCurrent();
        Long size = findTagArticlePageListReqVO.getSize();
        Long tagId = findTagArticlePageListReqVO.getId();

        //判断标签是否存在
        TagDO tagDO = tagMapper.selectById(tagId);
        if(Objects.isNull(tagDO)){
            log.warn("没有这个标签，tagId:{}",tagId);
            throw new BizException(ResponseCodeEnum.TAG_NOT_EXISTED);
        }

        //查询标签下关联的所有文章ID
        List<ArticleTagRelDO> articleTagRelDOS = articleTagRelMapper.selectByTagId(tagId);
        //判断标签下是否有文章
        if(CollectionUtils.isEmpty(articleTagRelDOS)){
            log.info("一篇都没有，tagId:{}",tagId);
            return PageResponse.success(null,null);
        }
        //提取所有文章ID
        List<Long> articleIds = articleTagRelDOS.stream().map(ArticleTagRelDO::getArticleId).collect(Collectors.toList());
        //根据ID查询文章分页数据
        Page<ArticleDO> page = articleMapper.selectPageListByArticleIds(current, size, articleIds);
        List<ArticleDO> articleDOS = page.getRecords();

        //DO转VO
        List<FindTagArticlePageListRspVO> vos = null;
        if(!CollectionUtils.isEmpty(articleDOS)){
            vos = articleDOS.stream()
                    .map(articleDO -> ArticleConvert.INSTANCE.convertDO2TagArticleVO(articleDO))
                    .collect(Collectors.toList());
        }

        return PageResponse.success(page,vos);
    }
}
